package com.caifu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caifu.bean.PageDo;
import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.mapper.BmCouponsRecordMapper;
import com.caifu.mapper.BmUserinfoMapper;
import com.caifu.pojo.BmCouponsRecord;
import com.caifu.pojo.BmUserinfo;
import com.caifu.util.Util;
import com.caifu.vo.userinfoVo.BmCouponsRecordForUserDetailVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author tianwenwen
 * ClassName: BmUserinfoService.java
 * version: 1.0.0
 * createTime: 2020年10月16日 10:59:58
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class BmUserinfoService {

    @Resource
    private BmUserinfoMapper bmUserinfoMapper;
    @Resource
    private BmCouponsRecordMapper bmCouponsRecordMapper;


    /**
     * 获取老人优惠券 基本信息
     *
     * @param userInfoId userInfoId
     * @param type       优惠券类型（1.理发2.缝纫）
     * @return BmUserinfo
     */
    public BmUserinfo getBmUserInfoByUserInfoId(String userInfoId, Integer type) {
        QueryWrapper<BmUserinfo> bmUserInfoQueryWrapper = new QueryWrapper<>();
        bmUserInfoQueryWrapper.eq("userinfo_id", userInfoId);
        bmUserInfoQueryWrapper.eq("type", type);
        bmUserInfoQueryWrapper.eq("status", "1");
        return bmUserinfoMapper.selectOne(bmUserInfoQueryWrapper);
    }

    /**
     * 获取老人优惠券 消费信息
     *
     * @param vo 查询条件
     * @return Result
     */
    public Result<Object> getBmCouponsRecordListOfUserInfoId(BmCouponsRecordForUserDetailVO vo) {
        //通过userNo 查询bm_userinfo
        QueryWrapper<BmUserinfo> bmUserInfoQueryWrapper = new QueryWrapper<>();
        bmUserInfoQueryWrapper.eq("userinfo_id", vo.getUserNo());
        bmUserInfoQueryWrapper.eq("status", "1");
        List<BmUserinfo> bmUserInfoList = bmUserinfoMapper.selectList(bmUserInfoQueryWrapper);
        //bm_userinfo    中获取到id 再去查询bm_coupons_record
        List<String> ids = bmUserInfoList.stream().map(model -> model.getId() + "").collect(Collectors.toList());

        //查询bm_coupons_record
        QueryWrapper<BmCouponsRecord> bmCouponsRecordQueryWrapper = new QueryWrapper<>();
        if (Util.isNotNull(vo.getStartTime()) && Util.isNotNull(vo.getEndTime())) {
            bmCouponsRecordQueryWrapper.between("created_time", vo.getStartTime() + " 00:00:00", vo.getEndTime() + " 23:59:59");
        }
        if (Util.isNotNull(vo.getType())) {
            bmCouponsRecordQueryWrapper.eq("type", vo.getType());
        }
        if (ids.size() == 0) {
            return ResultUtil.getPageDataSuccess(new PageDo<>(0L, 0L, 0, null));
        }
        bmCouponsRecordQueryWrapper.in("bm_userinfo_id", ids);
        bmCouponsRecordQueryWrapper.orderByDesc("created_time");
        IPage<BmCouponsRecord> iPage = bmCouponsRecordMapper.selectPage(new Page<>(vo.getCurrentPage(), vo.getPageSize()), bmCouponsRecordQueryWrapper);
        List<BmCouponsRecord> resultList = iPage.getRecords();
        //1.理发2.缝纫
        String fairType = "1";
        String sewType = "2";
        for (BmCouponsRecord record : resultList) {
            if (fairType.equals(record.getType())) {
                record.setType("理发券");
            } else if (sewType.equals(record.getType())) {
                record.setType("缝纫券");
            }
        }
        return ResultUtil.getPageDataSuccess(new PageDo<>(iPage.getCurrent(), iPage.getSize(), Integer.parseInt(iPage.getTotal() + ""), resultList));
    }


}
